package uniandes.dermatologia;

import java.util.Date;

import uniandes.dermatologia.models.Consultation;
import uniandes.dermatologia.resources.DBManager;
import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.support.v4.app.NavUtils;

public class RecordsActivity extends Activity implements OnItemClickListener {

	public static final String[] FROM = { DBManager.COL_ID,

	DBManager.COL_CREATED_AT };

	public static final int[] TO = { R.id.row_lbl_result, R.id.row_lbl_score };

	private ListView recordsList;

	private Cursor recordsCursor;

	private SimpleCursorAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_records);

		getActionBar().setDisplayHomeAsUpEnabled(false);

		recordsList = (ListView) findViewById(R.id.listRecords);

		recordsCursor = ((DermatologiaUniandesApp) getApplication())
				.getDbManager().queryConsultations();

		adapter = new SimpleCursorAdapter(this, R.layout.row_result,
				recordsCursor, FROM, TO);

		adapter.setViewBinder(VIEW_BINDER);
		recordsList.setAdapter(adapter);
		recordsList.setOnItemClickListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.records, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public static final ViewBinder VIEW_BINDER = new ViewBinder() {

		@Override
		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

			if (view.getId() == R.id.row_lbl_result) {

				((TextView) view).setText("Consult "
						+ cursor.getInt(columnIndex));

				return true;
			} else if (view.getId() == R.id.row_lbl_score) {

				long time = cursor.getLong(columnIndex);
				CharSequence relativeTime = DateUtils
						.getRelativeTimeSpanString(time);

				((TextView) view).setText(relativeTime);
				return true;
			}
			return false;

		}
	};

	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		recordsCursor.moveToPosition(position);
		Consultation c = new Consultation();

		c.setLocalDBId(recordsCursor.getInt(recordsCursor
				.getColumnIndex(DBManager.COL_ID)));

		c.setConsultationDate(new Date(recordsCursor.getLong(recordsCursor
				.getColumnIndex(DBManager.COL_CREATED_AT))));
		/*
		 * Symptom symptom = ((SkinHealthApp) getApplication()).getManejadorBD()
		 * .querySymptom(c.getLocalId()); Patient p = ((SkinHealthApp)
		 * getApplication()).getManejadorBD() .queryPatient(c.getLocalId());
		 * 
		 * c.setPaciente(p); c.setSymptom(symptom);
		 */

		((DermatologiaUniandesApp) getApplication()).setCurrentConsultation(c);

		Intent intent = new Intent(this, ResultsActivity.class);
		startActivity(intent);

	}
}
